package com.taotao.manage.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.taotao.common.pojo.EasyUIResult;
import com.taotao.manage.pojo.Content;
import com.taotao.manage.service.ContentService;

@Controller
@RequestMapping("content")
public class ContentController {

	@Autowired
	private ContentService contentService;

	/**
	 * 新增内容
	 * 
	 * @param content
	 * @return
	 */
	@PostMapping
	public ResponseEntity<Void> saveContent(Content content) {

		try {
			content.setId(null);
			this.contentService.saveSelective(content);
			return ResponseEntity.status(HttpStatus.CREATED).build();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}

	/**
	 * 根据内容分类查询内容
	 * @param categoryId
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@GetMapping
	public ResponseEntity<EasyUIResult<Content>> queryContentList(@RequestParam("categoryId") Long categoryId,
			@RequestParam(value = "page", defaultValue = "1") Integer pageNum,
			@RequestParam(value = "rows", defaultValue = "20") Integer pageSize) {

		try {
			EasyUIResult<Content> result = this.contentService.queryByPageOrderBy(categoryId, pageNum,
					pageSize);
			return ResponseEntity.ok(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}
}
